package top.jnaw.jee.platform.controller.basics.extend;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import top.jnaw.jee.platform.Consts;
import top.jnaw.jee.platform.model.JobManage;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Strings;

/**
 * Copyright (C) 2018 版权所有。 <p> 文件名： 文件功能描述：
 *
 * @author lc  2018/8/29 11:06 创建内容： <p> <p> 修改标识： 修改原因：
 */
public class JobManageController extends Controller {

  public void add() {
    ModelResult result = new ModelResult(Consts.Result.kBadParam);

    JSONObject req = Strings.toJSONObject(this);

    final String name = Strings.getString(req, "name", "");
    final String type = Strings.getString(req, "type", "");
    final String clazz = Strings.getString(req, "clazz", "");
    final String cron_expression = Strings
        .getString(req, "cron_expression", "");
    final String is_enabled = Strings.getString(req, "is_enabled", "N");

    if (StrKit.notBlank(name, type, clazz, cron_expression)) {
      result = JobManage.add(name, type, clazz, cron_expression, is_enabled);
    }

    renderJson(result.toJsonId());
  }

  public void update() {
    ModelResult result = new ModelResult(Consts.Result.kBadParam);

    JSONObject req = Strings.toJSONObject(this);

    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);
    final String name = Strings.getString(req, "name", "");
    final String type = Strings.getString(req, "type", "");
    final String clazz = Strings.getString(req, "clazz", "");
    final String cron_expression = Strings
        .getString(req, "cron_expression", "");
    final String is_enabled = Strings.getString(req, "is_enabled", "N");

    if (id > 0 && StrKit.notBlank(name, type, clazz, cron_expression)) {
      result = JobManage
          .update(id, name, type, clazz, cron_expression, is_enabled);
    }

    renderJson(result.toJsonId());
  }

  public void start() {
    ModelResult result = new ModelResult(Consts.Result.kBadParam);

    JSONObject req = Strings.toJSONObject(this);

    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);

    final String is_enabled = Strings.getString(req, "is_enabled", "N");

    if (id > 0) {
      result = JobManage.update(id, is_enabled);
    }

    renderJson(result.toJsonId());
  }

  public void delete() {
    ModelResult modelResult = new ModelResult();

    JSONObject req = Strings.toJSONObject(this);
    final int id = Strings.getInt(req, "id", Consts.DEFAULT_BAD_ID);

    if (JobManage.delete(id)) {
      modelResult.setResult(Consts.Result.kSuccess);
    }
    renderJson(modelResult.toJson());

  }

  public void list() {

    JSONObject req = Strings.toJSONObject(this);

    final int page = Strings.getInt(req, "page", Consts.DEFAULT_PAGE);
    final int size = Strings.getInt(req, "size", Consts.DEFAULT_PAGE_SIZE);
    final JSONArray order_by = Strings.getJSONArray(req, "order_by",null);

    ModelResult result = JobManage.list(page, size, order_by);

    renderJson(result.toJson());

  }
}
